package com.baiku.sanhos.controller;

import com.baiku.sanhos.mapper.UserChildMapper;
import com.baiku.sanhos.pojo.dto.UserChildDTO;
import com.baiku.sanhos.pojo.entity.User;
import com.baiku.sanhos.pojo.entity.UserChild;
import com.baiku.sanhos.pojo.vo.ChildNickNameVO;
import com.baiku.sanhos.pojo.vo.ChildUserVO;
import com.baiku.sanhos.response.ResultVO;
import com.baiku.sanhos.response.StatusCode;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ruoLi
 * 子用户相关业务
 */
@Api(tags = "子用户管理")
@RestController
@RequestMapping("/pa/userChild")
public class UserChildController {
    private final static Integer MAX_CHILD_COUNT=2;
    @Autowired
    UserChildMapper mapper;
    @ApiOperation("添加子用户")
    @PreAuthorize("hasAnyAuthority('Details')")
    @PostMapping("/insert")
    public ResultVO addUserChild(@RequestBody UserChildDTO userChildDTO){
        UserChild child=new UserChild();
        BeanUtils.copyProperties(userChildDTO,child);
        //TODO 业务规则
        QueryWrapper<UserChild> wrapper=new QueryWrapper<>();
        wrapper.eq("parent_user",child.getParentUser());
        Integer count = mapper.selectCount(wrapper);
        if (count<=MAX_CHILD_COUNT){
            mapper.insert(child);
            return ResultVO.ok();
        }
        return ResultVO.error(StatusCode.UPPER_LIMIT_CHILD);
    }
    @ApiOperation("根据用户id查询子用户")
    @PreAuthorize("hasAnyAuthority('Details')")
    @GetMapping("listUserChildByParentId/{parentUsername}")
    public ResultVO listUserChild(@PathVariable String parentUsername){
        QueryWrapper<UserChild> wrapper=new QueryWrapper<>();
        wrapper.eq("parent_user",parentUsername);
        List<UserChild> childList = mapper.selectList(wrapper);
        if (childList.isEmpty()){
            return ResultVO.error(StatusCode.DATA_NOT_FOUND);
        }
        return ResultVO.ok(childList);
    }

    @PreAuthorize("hasAnyAuthority('Details')")
    @GetMapping("/getUserStatus")
    public ResultVO getUserStatus(){
        return ResultVO.ok();
    }

    @PreAuthorize("hasAnyAuthority('Details')")
    @GetMapping("/listChildUsers/{parentUser}")
    public ResultVO listChildUsers(@PathVariable String parentUser){
        List<ChildNickNameVO> list = mapper.listChildUsers(parentUser);
        //用户账号查询子用户
        return ResultVO.ok(list);
    }
    @ApiOperation("根据父账号查询子用户昵称")
    @PreAuthorize("hasAnyAuthority('Details')")
    @GetMapping("/listPatientByUsername/{username}")
    public ResultVO listPatientByUsername(@PathVariable String username){
        List<ChildUserVO> list=mapper.listChildUsersAndParentUser(username);
        return ResultVO.ok(list);
    }
}
